/**
 * @format
 * @Author: TC
 * @Date: 2023-07-27 16:29:37
 * @Last Modified by:   TC
 * @Last Modified time: 2023-07-27 16:29:37
 */

import { Color, color, Material, renderer, RenderTexture, Texture2D, UIRenderer } from "cc";
import { nsBundleConst } from "../../Script/Const/BundleConst";
import { app } from "../App";
import { SingleBase } from "../Interface/SingleBase";
import { ASSERT_TYPE } from "../Module/Res/ResConst";

/**
 * 着色器管理类
 */
export class ShaderManager extends SingleBase {
    public static get instance(): ShaderManager {
        return ShaderManager._instance
            ? (ShaderManager._instance as ShaderManager)
            : (ShaderManager._instance = new ShaderManager());
    }

    constructor() {
        super();
    }

    public changeMaterial(urd: UIRenderer, mtlPath: string, cb: () => void): void {
        if (!urd) return;
        app.loaderMgr.loadAsset(nsBundleConst.Shader, mtlPath, ASSERT_TYPE.MATERIAL, (m: Material) => {
            urd.customMaterial = m;
            cb();
        });
    }

    /**
     * 重置材质属性
     * @param mtl
     */
    reset(mtl: Material): void {
        mtl.resetUniforms();
    }

    /**
     * 设置材质纹理属性
     * @param mtl
     * @param pro
     * @param tex
     * @returns
     */
    public setTextureProperty(mtl: Material, pro: string, tex: Texture2D | RenderTexture): void {
        mtl.setProperty(pro, tex);
    }

    /**
     * 设置材质数值属性
     * @param mtl
     * @param pro
     * @param num
     * @returns
     */
    public setNumberProperty(mtl: Material, pro: string, num: number): void {
        mtl.setProperty(pro, num);
    }

    /**
     * 设置材质颜色属性
     * @param mtl
     * @param pro
     * @param color
     * @returns
     */
    public setColorProperty(mtl: Material, pro: string, color: Color): void {
        mtl.setProperty(pro, color);
    }
}
